﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;
using System.Data.SqlClient;
using OneDrive.Model;
using Maticsoft.DBUtility;

namespace OneDrive.DAO
{
    public partial class HouseExtInfoDAO
    {
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("HouseID", "tbl_HouseExtInfo");
        }

        public static bool Clear()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_HouseExtInfo ");

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int iHouseID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbl_HouseExtInfo");
            strSql.Append(" where HouseID=@HouseID");
            SqlParameter[] parameters = {
                    new SqlParameter("@HouseID", SqlDbType.Int)
            };
            parameters[0].Value = iHouseID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HouseExtInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbl_HouseExtInfo(");
            strSql.Append("HouseID,SellingPoint,SellingReason)");
            strSql.Append(" values (");
            strSql.Append("@HouseID,@SellingPoint,@SellingReason,@Phone,@Rank,@PortertyFee,@Company,@Outlets)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@HouseID", SqlDbType.Int),
                    new SqlParameter("@SellingPoint", SqlDbType.NVarChar,200),
                    new SqlParameter("@SellingReason", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.HouseID;
            parameters[2].Value = model.SellingPoint;
            parameters[3].Value = model.SellingReason;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(HouseExtInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbl_HouseExtInfo set ");
            strSql.Append("HouseID=@HouseID,");
            strSql.Append("SellingPoint=@SellingPoint,");
            strSql.Append("SellingReason=@SellingReason,");

            SqlParameter[] parameters = {
                    new SqlParameter("@HouseID", SqlDbType.Int),
                    new SqlParameter("@SellingPoint", SqlDbType.NVarChar,200),
                    new SqlParameter("@SellingReason", SqlDbType.NVarChar,200)};

            parameters[1].Value = model.HouseID;
            parameters[2].Value = model.SellingPoint;
            parameters[3].Value = model.SellingReason;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int iHouseID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_HouseExtInfo ");
            strSql.Append(" where HouseID=@HouseID");
            SqlParameter[] parameters = {
                    new SqlParameter("@HouseID", SqlDbType.Int)
            };
            parameters[0].Value = iHouseID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string strHouseIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_HouseExtInfo ");
            strSql.Append(" where HouseID in (" + strHouseIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HouseExtInfo GetModel(int iHouseID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 HouseID,SellingPoint,SellingReason from tbl_HouseExtInfo ");
            strSql.Append(" where HouseID=@HouseID");
            SqlParameter[] parameters = {
                    new SqlParameter("@HouseID", SqlDbType.Int)
            };
            parameters[0].Value = iHouseID;

            HouseExtInfo model = new HouseExtInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HouseExtInfo DataRowToModel(DataRow row)
        {
            HouseExtInfo model = new HouseExtInfo();
            if (row != null)
            {
                if (row["HouseID"] != null && row["HouseID"].ToString() != "")
                {
                    model.HouseID = int.Parse(row["HouseID"].ToString());
                }
                if (row["HouseID"] != null)
                {
                    model.SellingPoint = row["HouseID"].ToString();
                }
                if (row["SellingReason"] != null)
                {
                    model.SellingReason = row["SellingReason"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select HouseID,SellingPoint,SellingReason ");
            strSql.Append(" FROM tbl_HouseExtInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" HouseID,SellingPoint,SellingReason ");
            strSql.Append(" FROM tbl_HouseExtInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbl_HouseExtInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.CID desc");
            }
            strSql.Append(")AS Row, T.*  from Course T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<HouseExtInfo> DataTableToList(DataTable dt)
        {
            List<HouseExtInfo> modelList = new List<HouseExtInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                HouseExtInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
        #endregion  BasicMethod

    }
}
